/**
 * Created by lvlq on 16/3/12.
 */
var date = Util("Func.date");
module.exports.index = function (req, res) {
    var openid = req.query.openid;
    var oid = req.query.oid;
    if (openid == oid) {
        res.redirect("/index/main?openid=" + oid);
        return
    }

    var a = 0, b = 0, c = 0;

    var zan = {
        "1": {},
        "2": {},
        "3": {}
    };

    //1 jd5 2jd30 3lxx
    var cart = {
        "0": {
            "0": {
                "0": 0,
                "1": 3
            },
            "1": {
                "0": 2,
                "1": 4
            }
        },
        "1": {
            "0": {
                "0": 1,
                "1": 5
            },
            "1": {
                "0": 6,
                "1": 7
            }
        }
    };

    var Record = M("index.record");
    var Prize = M("index.prize");

    Prize.findAll().then(function (prize_list) {
        for (var i = 0; i < prize_list.length; i++) {
            zan[prize_list[i].id].all = parseInt(prize_list[i].zan_num);
        }
        return Record.findAll({
            where: {
                openid: openid,
                pid: 1
            }
        })
    }).then(function (jd5) {
        zan["1"].num = jd5.length;
        if (zan["1"].num > zan["1"].all)
            zan["1"].num = zan["1"].all;
        if (zan["1"].num >= zan["1"].all) {
            a = 1;
        }
        return Record.findAll({
            where: {
                openid: openid,
                pid: 2
            }
        })
    }).then(function (jd30) {
        zan["2"].num = jd30.length;
        if (zan["2"].num > zan["2"].all)
            zan["2"].num = zan["2"].all;
        if (zan["2"].num >= zan["2"].all) {
            b = 1;
        }
        return Record.findAll({
            where: {
                openid: openid,
                pid: 3
            }
        });
    }).then(function (lxx) {
        zan["3"].num = lxx.length;
        if (zan["3"].num > zan["3"].all)
            zan["3"].num = zan["3"].all;
        if (zan["3"].num >= zan["3"].all) {
            c = 1;
        }
        var User = M("index.user");

        return User.find({
            where: {
                openid: openid
            }
        });
    }).then(function (user) {
        if (!user) throw new Error("1");
        user.nickname = new Buffer(user.nickname, 'base64').toString();
        res.render("share.html", {
            zan: zan,
            userinfo: user,
            buycart: cart[a][b][c]
        });
    }).catch(function (err) {
        console.log(err.message);
        res.send("系统错误~~请联系管理员!");
    });

};

module.exports.addZan = function (req, res) {
    var pid = req.body.pid;
    var openid = req.body.openid;
    var oid = req.body.oid;
    var today = date.getToday();
    var Record = M("index.record");
    var Prize = M("index.prize");
    var User = M("index.user");
    var pname;
    var zan_num;
    //User.find({
    //    where: {
    //        openid: oid
    //    }
    //}).then(function (user) {
    //    if (!user) {
    //        throw new Error("系统错误");
    //    }
    //
    //    return Prize.find({
    //        where: {
    //            id: pid
    //        }
    //    })
    //})

    Prize.find({
        where: {
            id: pid
        }
    }).then(function (prize) {
        if (!prize) {
            throw new Error("系统错误");
        }
        zan_num = prize.zan_num;
        pname = prize.name;
        return Record.find({
            where: {
                openid: openid,
                oid: oid
            }
        });
    }).then(function (data) {
        if (data) {
            throw new Error("您已经点过了");
        }


        return Record.findAll({
            where: {
                openid: openid,
                pid: pid
            }
        });


    }).then(function (rlist) {
        var count = rlist.length;
        if (count >= zan_num) {
            throw new Error("此项已满!无法继续点赞");
        }

        return Record.create({
            openid: openid,
            pid: pid,
            oid: oid,
            today: today,
            pname: pname
        })
    }).then(function () {
        res.json({
            errno: 0
        });
    }).catch(function (err) {
        res.json({
            errno: 1001,
            errmsg: err.message
        });
    });
};